{extend name="common@create" /}

{block name="form"}
    <form action="{$list.set.url}" method="{$list.set.formpost}" class="layui-form layui-form-pane" lay-filter="myform">

          <div class="layui-form-item">
            <label for="school" class="layui-form-label"><span class="x-red">*</span>学校</label>
            <div class="layui-input-block">
              {php} $schoollist = schlist('校级','校级');{/php}
              <select name="school" id="school" lay-verify="required">
                <option value=""></option>
                {volist name="schoollist" id="vo"}
                  <option value="{$vo.id}"
                  {present name="$list.data.school"}
                    {eq name="$list.data.school" value="$vo.id" }
                      selected
                    {/eq}
                  {/present}>
                    {$vo.title}
                  </option>
                {/volist}
              </select>
            </div>
          </div>
          <div class="layui-form-item">
            <label for="ruxuenian" class="layui-form-label">
              <span class="x-red">*</span>年级
            </label>
            <div class="layui-input-block">
              {php} $njlist = nianjiList();{/php}
              <select name="ruxuenian" id="ruxuenian" lay-verify="required" class="layui-form-label">
                <option value=""></option>
                {foreach $njlist as $key=>$vo}
                	<option value="{$key}" {present name="$list.data.stu_banji"}{eq name="$list.data.stu_banji.ruxuenian" value="$key" }selected{/eq}{/present}>
                		{$vo}
                	</option>
                {/foreach}
              </select>
            </div>
          </div>
          <div class="layui-form-item">
            <label for="banji" class="layui-form-label">
              <span class="x-red">*</span>班级
            </label>
            <div class="layui-input-block" id="banjidiv">
            </div>
          </div>
          <input type="hidden" id="kaoshi" name="kaoshi" value="{$list.set.kaoshi}">
          <div class="layui-form-item" style="text-align: right;">
              <button  class="layui-btn" lay-filter="" lay-submit="">
                  {$list.set.butname}
              </button>
          </div>
      </form>
{/block}

{block name="myjs"}
<script>
layui.use(['form'], function(){
	var form = layui.form;


	// 监听选择按钮
	form.on('select', function(data){
	  var myselectname = $(data.elem).attr('name');
	  if(myselectname == 'school' || myselectname == 'ruxuenian'){
	  	mybanji();
	  }
	});

  // 监听班级复选框
  form.on('checkbox(selectall)', function(data){
    if(data.elem.checked == true)
    {
      $(data.elem).nextAll('input').each(function()
        {
          $(this).attr('checked',true);
        });
    }else{
      $(data.elem).nextAll('input').each(function()
        {
          $(this).attr('checked',false);
        });
    }

    form.render('checkbox');
  });        

	//获取班级列表并更新
	function mybanji(){
		// 获取表单值
		var school = $('#school').val()
		,ruxuenian = $('#ruxuenian').val();
		
		// 获取节点
		var bj = $("#banjidiv");

		// 删除原来已有值
		$(bj).children().remove();
		// 获取班级列表
		$.ajax({
		    url:"/banji/njbanji",
		    type:"POST",
		    data:{
		    	school:school
		    	,ruxuenian:ruxuenian
		    },
		    dataType:'json',
		    success:function(result){
          $(bj).append('<input type="checkbox" name="banji[]" title="全选" value="" lay-skin="primary" onclick="selectAll(this)" lay-filter="selectall">');
		    	// if(result.length > 0){
		    		result.forEach(function(val,index){
              $(bj).append('<input type="checkbox" name="banji[]" title="'+val['banTitle']+'" value="'+val['id']+'" lay-skin="primary">');
		    		})
		    	// }
          form.render('checkbox');
		    },
		    error:function(xhr,status,error) {
		      layer.msg('数据处理错误',{
		          icon: 2,
		          time: 2000 //2秒关闭（如果不配置，默认是3秒）
		      });
		    }
		});
  }
});
</script>
{/block}

